package com.CST.service.impl.ys;

import com.CST.common.BaseEntity;
import com.CST.dto.ys.YsDictionaryDTO;
import com.CST.entity.ys.YsDictionary;
import com.CST.mapper.cst.ys.YsDictionaryMapper;
import com.CST.service.ys.YsDictionaryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author qtx
 * @since 2023-05-07
 */
@Service
public class YsDictionaryServiceImpl extends ServiceImpl<YsDictionaryMapper, YsDictionary> implements YsDictionaryService {

    @Override
    public IPage<YsDictionary> listYsDictionaryPage(YsDictionaryDTO dto) {
        Boolean aBoolean = Optional.of(dto.isSort())
                .orElse(Boolean.TRUE);
        return page(dto.getPage(), Wrappers.lambdaQuery(YsDictionary.class)
                .eq(BaseEntity::getDeleteFlag, Boolean.FALSE)
                .like(StringUtils.isNotBlank(dto.getDictionaryName()), YsDictionary::getDictionaryName,
                        dto.getDictionaryName())
                .eq(dto.getParent() != null, YsDictionary::getParent, dto.getParent())
                .eq(dto.getParent() == null, YsDictionary::getParent, "0")
                .orderBy(Boolean.TRUE, aBoolean, YsDictionary::getOrderNum));
    }

    @Override
    public List<YsDictionary> listYsDictionary(String selectType, String name) {
        return list(Wrappers.lambdaQuery(YsDictionary.class)
                .eq(BaseEntity::getDeleteFlag, Boolean.FALSE)
                .eq(YsDictionary::getSelectType, selectType)
                .eq(YsDictionary::getStatus, Boolean.TRUE)
                .like(StringUtils.isNotBlank(name), YsDictionary::getDictionaryName, name));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateYsDictionary(YsDictionary entity) {
        Optional.ofNullable(entity.getDictionaryCode())
                .orElseThrow(() -> new RuntimeException("部门编码为空。"));
        YsDictionary one = getOne(Wrappers.lambdaQuery(YsDictionary.class)
                .eq(BaseEntity::getDeleteFlag, Boolean.FALSE)
                .eq(YsDictionary::getDictionaryCode, entity.getDictionaryCode())
                .eq(entity.getParent() != null, YsDictionary::getParent, entity.getParent())
                .ne(entity.getId() != null, BaseEntity::getId, entity.getId()));
        if (Objects.isNull(one)) {
            if (entity.getParent() != null) {
                List<YsDictionary> ysDictionaries = list(Wrappers.lambdaQuery(YsDictionary.class)
                        .eq(YsDictionary::getParent, entity.getParent())
                        .eq(BaseEntity::getDeleteFlag, Boolean.FALSE));
                if (ysDictionaries.isEmpty()) {
                    ysDictionaries.add(YsDictionary.builder()
                            .selectType(UUID.randomUUID()
                                    .toString())
                            .build());
                }
                entity.setSelectType(ysDictionaries.get(0)
                        .getSelectType());
            }
            return saveOrUpdate(entity);
        } else {
            throw new RuntimeException("字典编码重复。");
        }
    }

    @Override
    public boolean updateStatus(Integer id) {
        YsDictionary getById = getById(id);
        YsDictionary ysDictionary = Optional.ofNullable(getById)
                .orElseThrow(() -> new RuntimeException("字典不存在"));
        boolean equals = ysDictionary.getParent()
                .equals(0);
        Boolean status = ysDictionary.getStatus();
        ysDictionary.setStatus(!status);
        if (equals && status) {
            update(Wrappers.lambdaUpdate(YsDictionary.class)
                    .set(YsDictionary::getStatus, Boolean.FALSE)
                    .eq(YsDictionary::getParent, ysDictionary.getId())
                    .eq(BaseEntity::getDeleteFlag, Boolean.FALSE));

        }
        return updateById(ysDictionary);
    }

    @Override
    public boolean removeYsDictionaryById(Integer id) {
        YsDictionary getById = getById(id);
        YsDictionary ysDictionary = Optional.ofNullable(getById)
                .orElseThrow(() -> new RuntimeException("字典不存在"));
        boolean equals = ysDictionary.getParent()
                .equals(0);
        ysDictionary.setDeleteFlag(Boolean.TRUE);
        if (equals) {
            update(Wrappers.lambdaUpdate(YsDictionary.class)
                    .set(YsDictionary::getDeleteFlag, Boolean.TRUE)
                    .eq(YsDictionary::getParent, ysDictionary.getId())
                    .eq(BaseEntity::getDeleteFlag, Boolean.FALSE));
        }
        return updateById(ysDictionary);
    }
}
